package com.interceptors;

import com.alibaba.fastjson.JSONObject;
import com.controller.LoginController;
import com.entity.UpdateKiaRaResBody;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;

/**
 * @author WZ
 * @version 1.0
 * @date 2021/8/20 10:49
 */
@Component

public class FirstInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//       response.setHeader("Access-Control-Allow-Origin","*");
//      response.setHeader("Access-Control-Allow-Credentials","true");
//       response.setHeader("Access-Control-Allow-Headers","Origin, X-Requested-With, Content-Type, Accept, Connection, User-Agent, Cookie");
        if("OPTIONS".equals(request.getMethod().toUpperCase())) {
            System.out.println("Method:OPTIONS");
            return true;
        }else {
            String token=request.getHeader("token");
            if(LoginController.existToken(token)){
                return true;
            }else {
                PrintWriter writer=null;
//                 ObjectMapper jsonObject=new ObjectMapper();
////
                long currenttime=System.currentTimeMillis();
                String date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(currenttime);
//                 tokenRes("401","未登录或登录已过期",date1);
////
//                 String json =jsonObject.writeValueAsString(new UpdateKiaRaResBody("401","未登录或登录已过期",date1));

                response.setCharacterEncoding("UTF-8");
                response.setContentType("text/Json; charset=utf-8");
                try {
                    JSONObject res = new JSONObject();

                    res.put("msg","未登录或登录已过期");
                    res.put("code","401");
                    res.put("date",date1);
                    writer = response.getWriter();
                    writer.append(res.toString());
//                     writer=response.getWriter();
//                     writer.print(json);
                }catch (IOException e){
                }finally {
                    if (writer!=null)
                        writer.close();
                }
                return  false;
            }
        }




    }


    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//        System.out.println("FirstInterceptor+postHandle");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//        System.out.println("FirstInterceptor+afterCompletion");
    }
}
